package com.cch.cooperation.api.common.util;

import net.coobird.thumbnailator.Thumbnails;

import java.io.*;

public class ImgUtil {

    public static void compress(InputStream inputStream, OutputStream outputStream, int w, int h) throws IOException {
        try {
            Thumbnails.of(inputStream).width(w).height(h).keepAspectRatio(true).toOutputStream(outputStream);
        } finally {
            inputStream.close();
            outputStream.close();
        }
    }

    public static byte[] compress(byte[] buff, int w, int h) throws IOException {
        try(ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            Thumbnails.of(new ByteArrayInputStream(buff)).width(w).height(h).keepAspectRatio(true).toOutputStream(os);
            return os.toByteArray();
        }
    }

    public static byte[] compress(InputStream inputStream, int w, int h) throws IOException {
        try(ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            Thumbnails.of(inputStream).width(w).height(h).keepAspectRatio(true).toOutputStream(os);
            return os.toByteArray();
        } finally {
            inputStream.close();
        }
    }

}
